﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace Complaint.Models.Dtos.JavascriptConverters
{
    public class ContactConverter : JavaScriptConverter
    {
        #region Overrides of JavaScriptConverter

        /// <summary>
        /// When overridden in a derived class, converts the provided dictionary into an object of the specified type.
        /// </summary>
        /// <returns>
        /// The deserialized object.
        /// </returns>
        /// <param name="dictionary">An <see cref="T:System.Collections.Generic.IDictionary`2"/> instance of property data stored as name/value pairs.</param><param name="type">The type of the resulting object.</param><param name="serializer">The <see cref="T:System.Web.Script.Serialization.JavaScriptSerializer"/> instance.</param>
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            var o = new Contact
                        {
                            ContactID = Utility.GetDictionaryValueJson<int>(dictionary, "ContactID"),
                            Title = Utility.GetDictionaryValueJson<string>(dictionary, "Title"),
                            FirstName = Utility.GetDictionaryValueJson<string>(dictionary, "FirstName"),
                            LastName = Utility.GetDictionaryValueJson<string>(dictionary, "LastName"),
                            JobTitle = Utility.GetDictionaryValueJson<string>(dictionary, "JobTitle"),
                            Email = Utility.GetDictionaryValueJson<string>(dictionary, "Email")
                        };

            return o;
        }

        /// <summary>
        /// When overridden in a derived class, builds a dictionary of name/value pairs.
        /// </summary>
        /// <returns>
        /// An object that contains key/value pairs that represent the object’s data.
        /// </returns>
        /// <param name="obj">The object to serialize.</param><param name="serializer">The object that is responsible for the serialization.</param>
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            var serialized = new Dictionary<string, object>();
            var o = obj as Contact;
            if (o == null)
            {
                throw new ArgumentException("Passed object is not of the correct type (Response expected).", "obj");
            }

            serialized.Add("ContactID", o.ContactID);
            serialized.Add("Title", o.Title);
            serialized.Add("FirstName", o.FirstName);
            serialized.Add("LastName", o.LastName);
            serialized.Add("JobTitle", o.JobTitle);
            serialized.Add("Email", o.Email);

            return serialized;
        }

        /// <summary>
        /// When overridden in a derived class, gets a collection of the supported types.
        /// </summary>
        /// <returns>
        /// An object that implements <see cref="T:System.Collections.Generic.IEnumerable`1"/> that represents the types supported by the converter.
        /// </returns>
        public override IEnumerable<Type> SupportedTypes
        {
            get
            {
                return new[] { typeof(Contact) };
            }
        }

        #endregion
    }
}